home *** CD-ROM | disk | FTP | other *** search
- Path: dharma.hip.berkeley.edu!user
- From: genecutl@mendel.berkeley.edu (gc)
- Newsgroups: comp.lang.c
- Subject: basic question about pointers and local vars
- Date: 16 Feb 1996 18:18:18 GMT
- Organization: worl domination
- Message-ID: <genecutl-1602961021230001@dharma.hip.berkeley.edu>
- NNTP-Posting-Host: dharma.hip.berkeley.edu
- X-Newsreader: Value-Added NewsWatcher 2.0d31h+
-
- This is basic and I should know it but I don't. Here's my query--
- So I know that if you have:
-
- main () {
- char string[10];
- function(string);
- ...
- }
- function (char *string) {
- string[0] = 'a';
- string[1] = 'b';
- ....
- }
-
- This changes what the string is in main because string is a pointer. But
- what is the
- standard way to get a string returned by a function if you don't want to
- allocate
- space for the array in main. For example, is this valid:
-
- main() {
- char *string;
- function(string);
- ...
- }
- function(char *string) {
- string = (char *)malloc(...);
- string[0] = 'a';
- string[1] = 'b';
- }
-
- It doesn't seem like it should be because the value of the pointer itself
- gets changed
- here and I would have thought that that gets lost when control goes back
- to main, but
- it does work when I try it. So where is the problem and what is the right
- way to do this?
- Thanks.
-
- --Gene Cutler
- genecutl@sp1.berkeley.edu
- TheRoadToNoWhere
- http://sp1.berkeley.edu/
-